package game;

//import java.io.BufferedReader;
//import java.io.InputStreamReader;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JOptionPane;

public class Game implements Observer {

	private ArrayList<Player> players;
	private Player me;
	private GamePlayer gp;
	private Client client;
	private ScoreList slist;
	private Screen screen;

	public Game() {
		this.players = new ArrayList<Player>();
		
		String in = JOptionPane.showInputDialog("Indtast dit spillernavn");
		String inIp = JOptionPane.showInputDialog("Indtast server-ip");
		
		this.me = new Player(in);
		players.add(me);
		
		this.slist = new ScoreList(players);
		slist.setVisible(true);
		
		this.screen = new Screen(GamePlayer.getMap(), me.getXpos(), me.getYpos());


		
		this.gp = new GamePlayer(this);
		
		this.client = new Client(this, inIp);
		
		Thread t = new Thread(new Runnable() {
			@Override
			public void run() {
				while(true)
					update(client.sendUpdate());
			}
		});
		t.start();		
	}

	/**
	 * 
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception { 
		new Game(); 
	}
	
	public GamePlayer getGamePlayer() {
		return gp;
	}

	public ArrayList<Player> getPlayers() {
		return players;
	}


	public void setPlayers(ArrayList<Player> players) {
		this.players = players;
	}


	public Player getMe() {
		return me;
	}


	public void setMe(Player me) {
		this.me = me;
	}


	public GamePlayer getGp() {
		return gp;
	}


	public void setGp(GamePlayer gp) {
		this.gp = gp;
	}


	public Client getClient() {
		return client;
	}


	public void setClient(Client client) {
		this.client = client;
	}


	public ScoreList getSlist() {
		return slist;
	}


	public void setSlist(ScoreList slist) {
		this.slist = slist;
	}

	@Override
	public void update(ArrayList<Player> players) {
		this.players = players;
		for(Player p : this.players) {
			if (p.getName().equals(me.getName())) {
				me = p;
				break;
			}
		}
		
		screen.update(this.players);
		slist.update(this.players);
	}

	public Screen getScreen() {
		return screen;
	}


	public void setScreen(Screen screen) {
		this.screen = screen;
	}
}
